package com.example.travel.controller;

import com.example.travel.bean.ActivityBean;
import com.example.travel.service.ActivityService;
import com.example.travel.bean.SiteBean;
import com.example.travel.utils.DateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.List;
import java.util.Map;

@Controller
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @RequestMapping("/getActivityList")
    public ModelAndView getActivityList(@RequestParam(required = false) String searchKeyword,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pageSize) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        
        // 获取分页数据和总数
        List<ActivityBean> dataList = activityService.getActivityListWithPagination(searchKeyword, offset, pageSize);
        int totalCount = activityService.getActivityCount(searchKeyword);
        
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("totalCount", totalCount);
        modelAndView.addObject("currentPage", page);
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/deleteActivity", method= RequestMethod.POST)
    public ModelAndView deleteActivity(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int activityID = Integer.parseInt(params.get("activityID").toString());
        int status = activityService.deleteActivity(activityID);
        List<ActivityBean> dataList = activityService.getActivityList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/addActivity", method=RequestMethod.POST)
    public ModelAndView addActivity(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        String activityTitle = params.get("activityTitle").toString();
        String activityDesc = params.get("activityDesc").toString();
        String activityAuthor = params.get("activityAuthor").toString();
        String activityPicture = params.get("activityPicture").toString();
        String activityPrice = params.get("activityPrice").toString();
        String activityPhone = params.get("activityPhone").toString();
        String activityStar = params.get("activityStar").toString();
        String activityAddress = params.get("activityAddress").toString();
        String activityDate = params.get("activityDate").toString();

        activityPicture = activityPicture.isEmpty() ? "http://img.mp.itc.cn/upload/20170703/9f5f4a5cdd3845409d704b76dfed0663_th.jpg" : activityPicture;
        ActivityBean activityBean = new ActivityBean();
        activityBean.setActivityTitle(activityTitle);
        activityBean.setActivityStar(activityStar);
        activityBean.setActivityPrice(activityPrice);
        activityBean.setActivityDesc(activityDesc);
        activityBean.setActivityPicture(activityPicture);
        activityBean.setActivityAuthor(activityAuthor);
        activityBean.setActivityPhone(activityPhone);
        activityBean.setActivityAddress(activityAddress);
        activityBean.setActivityStatus("normal");
        activityBean.setActivityDate(activityDate);

        DateFormatter dateFormatter = new DateFormatter();
        String activityOnDate = dateFormatter.getDate();//获取当前日期作为发布时间
        activityBean.setActivityOnDate(activityOnDate);

        int status = activityService.addActivity(activityBean);
        List<ActivityBean> dataList = activityService.getActivityList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value="/updateActivity", method=RequestMethod.POST)
    public ModelAndView updateActivity(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        // 从参数中获取活动ID
        int activityID = Integer.parseInt(params.get("activityID").toString());
        String activityTitle = params.get("activityTitle").toString();
        String activityDesc = params.get("activityDesc").toString();
        String activityAuthor = params.get("activityAuthor").toString();
        String activityPicture = params.get("activityPicture").toString();
        String activityPrice = params.get("activityPrice").toString();
        String activityPhone = params.get("activityPhone").toString();
        String activityStar = params.get("activityStar").toString();
        String activityAddress = params.get("activityAddress").toString();
        String activityDate = params.get("activityDate").toString();
        // 设置默认图片（如果未提供）
        activityPicture = activityPicture.isEmpty() ? "http://img.mp.itc.cn/upload/20170703/9f5f4a5cdd3845409d704b76dfed0663_th.jpg" : activityPicture;

        // 创建并填充ActivityBean
        ActivityBean activityBean = new ActivityBean();
        activityBean.setActivityID(activityID);
        activityBean.setActivityTitle(activityTitle);
        activityBean.setActivityDesc(activityDesc);
        activityBean.setActivityAuthor(activityAuthor);
        //activityBean.setActivityPicture(activityPicture);
        activityBean.setActivityPrice(activityPrice);
        activityBean.setActivityPhone(activityPhone);
        activityBean.setActivityStar(activityStar);
        activityBean.setActivityAddress(activityAddress);
        activityBean.setActivityDate(activityDate);

        DateFormatter dateFormatter = new DateFormatter();
        String activityOnDate = dateFormatter.getDate();//获取当前日期作为发布时间
        activityBean.setActivityOnDate(activityOnDate);

        // 调用服务层更新活动
        int status = activityService.updateActivity(activityBean);

        // 获取更新后的活动列表
        List<ActivityBean> dataList = activityService.getActivityList();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", "success");

        return modelAndView;
    }


}
